const adminUsersModel = require('../model/adminUsersModel.js');
const admincategoriesModel = require('../model/admin_categoriesModel.js')
const adminPostsModel = require('../model/adminPostsModel');
const moment = require('moment');
module.exports = {
  getIndex(req,res){
    res.render('index',{});
  },
  getAdminIndex(req,res){
    // console.log(req.session);
    if(req.session.isLogin){
      res.render('admin/index',{});
    }else{
      res.send('<script>location.href = "/admin/login";</script>');
    }
  },
  getList(req,res){
    res.render('list');
  },
  getDetail(req,res){
    res.render('detail');
  },
  getAdminCategories(req,res){
    admincategoriesModel.getCategories((err,result)=>{
      if(err) console.error(err);
      let resObj = {
       code: 401,
       msg: '获取失败',
      };
      if(result){
        resObj.code = 200;
        resObj.msg = '获取成功';
      }
      res.render('admin/categories',{arr:result});
    });
  },
  getAdminLogin(req,res){
    res.render('admin/login');
  },
  getAdminComments(req,res){
    res.render('admin/comments');
  },
  getAdminPasswordReset(req,res){
    res.render('admin/password-reset');
  },  
  getAdminpostAdd(req,res){
    res.render('admin/post-add');
  },
  getAdminPosts(req,res){
    // if(req.session.isLogin){
    //   res.render('admin/posts');
    // }else{
    //   res.send('<script>location.href = "/admin/login";</script>');
    // }
  //   adminPostsModel.getPostsAll((err,result)=>{
  //      if(err) console.log(err);
  //      if(result){
  // // 修改时间日期。遍历
  //   result.forEach(e => {
  //     e.created = moment(e.create).format('YYYY-MM-DD HH:mm:ss');
  //   });
  //        res.render('admin/posts',{arr:result});
  //      }
  //   });
  // 使用浏览器请求数据
  res.render('admin/posts');
  },
  getAdminProfile(req,res){
    res.render('admin/profile');
  },
  getAdminSetting(req,res){
    res.render('admin/setting');
  },
  getAdminSlides(req,res){
    res.render('admin/slides');
  },
  getAdminUsers(req,res){
    adminUsersModel.getAllUsers((err,result)=>{
      if(err) console.error(err);
      if(result){
        res.render('admin/users',{arr:result});
      }
    });
  },  
  getAdminSettings(req,res){
    res.render('admin/settings');
  },


};